package it.infodea.tapestrydea.components;

import it.infodea.tapestrydea.services.pageinfo.bean.PageInfoBean;
import it.infodea.tapestrydea.support.annotations.PageInfo;
import it.infodea.tapestrydea.support.enumerations.SecurityGroup;

import java.util.List;

import org.apache.tapestry5.ComponentResources;
import org.apache.tapestry5.annotations.Parameter;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.annotations.SetupRender;
import org.apache.tapestry5.ioc.Messages;
import org.apache.tapestry5.ioc.annotations.Inject;

public class PageInfoCategoryRenderDea {

	private static final String DEA_MENU_LABEL = "-dea-menu-label";

	@Parameter(required = true)
	private SecurityGroup securityGroup;

	@Parameter(required = true)
	private List<PageInfoBean> pageInfos;

	@Parameter
	private String category;

	@SuppressWarnings("unused")
	@Property(write = false)
	@Parameter
	private int level = 0;

	@Inject
	private ComponentResources resources;

	@Inject
	private Messages messages;

	private PageInfoBean page;

	@SetupRender
	boolean initMenu() {
		if (securityGroup == null) {
			securityGroup = SecurityGroup.ANONYMOUS;
		}
		return pageInfos != null && pageInfos.size() > 0;
	}

	private String categoryRootFilter(String category) {
		return (PageInfo.CATEGORY_ROOT.equals(category)) ? "" : category;
	}

	public String getPageLabel() {
		String label = categoryRootFilter(page.getParentCategory()) + "_" + categoryRootFilter(page.getCategory()) + "_" + page.getLabel()
				+ DEA_MENU_LABEL;
		return messages.get(label);
	}

	public boolean isThisPage() {
		String currentPage = resources.getPageName();
		String testPageName = page.getLink();
		return (currentPage.equals(testPageName));
	}

	public boolean isRoot() {
		return PageInfo.CATEGORY_ROOT.equals(page.getCategory());
	}

	public List<PageInfoBean> getPageInfos() {
		return pageInfos;
	}

	public PageInfoBean getPage() {
		return page;
	}

	public void setPage(PageInfoBean page) {
		this.page = page;
	}

	public Object getContext() {
		return page.getContext();
	}

	public Object getPageLink() {
		return page.getLink();
	}

	public String getCategoryLabel() {
		return messages.get(category + DEA_MENU_LABEL);
	}

}
